//
//  PIODetailViewController.m
//  FootPrint
//
//  Created by alex tao on 4/8/13.
//  Copyright (c) 2013 alex tao. All rights reserved.
//

#import "PIODetailViewController.h"
#import "EventDetailViewController.h"

@interface PIODetailViewController (){

    MerchantMessage*  _merchant;
}

@end

@implementation PIODetailViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        _favParser = [[FavMerchantParser alloc]init];
        _favParser.delegate = self;
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    UIImage* imgNormal =[[UIImage imageNamed:@"btn_bg_normal.png"] stretchableImageWithLeftCapWidth:10 topCapHeight:0];
    UIImage* imgPress =[[UIImage imageNamed:@"btn_bg_press.png"] stretchableImageWithLeftCapWidth:10 topCapHeight:0];
    
    [_btnChoose setBackgroundImage:imgNormal forState:UIControlStateNormal];
    [_btnChoose setBackgroundImage:imgPress forState:UIControlStateHighlighted];
    
    [_btnChoose1 setBackgroundImage:imgNormal forState:UIControlStateNormal];
    [_btnChoose1 setBackgroundImage:imgPress forState:UIControlStateHighlighted];
    
    [_btnFav setBackgroundImage:imgNormal forState:UIControlStateNormal];
    [_btnFav setBackgroundImage:imgPress forState:UIControlStateHighlighted];

}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)dealloc {
    [_rawData release];
    [_poiIcon release];
    [_poiName release];
    [_poiAddress release];
    [_btnChoose release];
    [_btnFav release];
    [_btnChoose1 release];
    [super dealloc];
}
- (void)viewDidUnload {
    [self setPoiIcon:nil];
    [self setPoiName:nil];
    [self setPoiAddress:nil];
    [self setBtnChoose:nil];
    [self setBtnFav:nil];
    [self setBtnChoose1:nil];
    [super viewDidUnload];
}

- (IBAction)exit {
    if (self.navigationController.viewControllers.count > 1) {
        [self.navigationController popViewControllerAnimated:YES];
    } else {
        [self.navigationController dismissModalViewControllerAnimated:YES];
    }
}

- (IBAction)createEvent:(id)sender {
    EventDetailViewController * detail = [[[EventDetailViewController alloc] initWithNibName:@"EventDetailViewController" bundle:nil] autorelease];
    
    UINavigationController * navVc = self.navigationController;
    if (nil == navVc) {
        navVc = [[[UINavigationController alloc] initWithRootViewController:detail] autorelease];
        navVc.navigationBarHidden = YES;
        [ROOT_VC presentModalViewController:navVc animated:YES];
    } else {
        [navVc pushViewController:detail animated:YES];
    }

    [detail createWithPOI:_merchant];
}

- (IBAction)actionChoose:(id)sender {
    [_selDelegate didSelPOI:_merchant];
}

- (IBAction)actionFav:(id)sender {
    [_favParser reqAddFav:[GConfig getUserId] merId:[_merchant id]];
    _needBack = YES;
}

- (void)updatePOIInfo:(MerchantMessage*)merchant
{
    _merchant = merchant;
    if (!merchant.logo.isSys) {
        [_poiIcon loadAsyncImage:merchant.logo.imageUrl];
    } else {
        [_poiIcon setSysImageWithId:merchant.logo.id];
    }

    _poiName.text = merchant.name ? [NSString stringWithFormat:@"%@", merchant.name] : @"";
    _poiAddress.text = merchant.address ? [NSString stringWithFormat:@"%@", merchant.address] : @"";
}


//请求收藏
- (void)didFinishReqFav:(UserFavoriteMerchantQueryResponseMessage*)favMerchants{

}
- (void)failReqFav{

}

//添加商户
- (void)didFinishAddMer:(CustomizedMerchantResponseMessage*)addResp{

}
- (void)failAddMer{

}

//添加收藏
- (void)didFinishAddFav:(UserFavoriteMerchantResponseMessage*)addFavResp{
    
    if(addFavResp != nil && [addFavResp resultCode] == 0){
        [[[iToast makeText:@"已添加到收藏"] setDuration:2000] show];
        if(_needBack){
            [_selDelegate didSelPOI:_merchant];
        }
        [[NSNotificationCenter defaultCenter] postNotificationName:NOTIFY_REFRESH_FAV object:nil];
        return;
    }
    [[[iToast makeText:@"服务器出错"] setDuration:2000] show];
}
- (void)failAddFav{
    [[[iToast makeText:@"服务器出错"] setDuration:2000] show];
}


//删除收藏
- (void)didFinishDel:(UserFavoriteMerchantDelResponseMessage*)delFavResp{

}
-(void)failDelFav{

}

@end
